package com.zfk.transaction.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zfk.transaction.entity.ToEmail;
import com.zfk.transaction.entity.User;
import com.zfk.transaction.service.RedisService;
import com.zfk.transaction.service.UserService;
import com.zfk.transaction.service.VerificationService;
import com.zfk.transaction.util.EmailUtil;
import com.zfk.transaction.util.Response;
import com.zfk.transaction.util.enums.RequestType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Map;

/**
 * Author : zfk
 * Date : 10:39
 */

@Api("LoginController")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private VerificationService verificationService;

    @Autowired
    private RedisService redisService;

    @PostMapping
    @ApiOperation("登录")
    public Response login(@RequestBody Map<String,String> jsonString){

        System.out.println(jsonString);
        User user = userService.login((String) jsonString.get("email"), (String)jsonString.get("password"));
        System.out.println(user);
        if (user != null){
            return Response.ok(user);
        }
        return Response.error(RequestType.LOGIN_FAIL);
    }

    /**
     * 通过邮箱发送验证码
     * @param email
     * @return
     */
    @GetMapping("/getVerificationCode")
    @ApiOperation("活动验证码")
    public Response getVerificationCode(@RequestParam String email){

        // 生成 验证码 ，封装ToMail对象
        String code = EmailUtil.generateCheckCode();
        // 往redis存放一分钟
        redisService.set("code"+email,code);

        boolean result = verificationService.sendVerfication(new ToEmail(email, "验证码", "验证码：" + code));

        if (result){
            return Response.ok();
        }else {
            return Response.error("发送邮件失败");
        }

    }


    @PostMapping("/register")
    @ApiOperation("注册账号")
    public Response register(@RequestBody Map<String,Object> jsonString){

        // 输入的验证码
        String code =  jsonString.get("code").toString();

        Object userJson = jsonString.get("user");


        String string = JSON.toJSONString(userJson);

        Map<String,String> map = JSON.parseObject(string, Map.class);

        User user = new User(0,map.get("email"),map.get("userName"),map.get("password"),map.get("phone"));

        // 得到redis中存放的验证码
        String redisCode = redisService.get("code"+user.getEmail());
        // redisCode为空或者 验证码不对
        if(redisCode == null || !redisCode.equals(code) ){
            return Response.error("验证码错误或者无效");
        }

        Integer add = userService.add(user);

        if (add == 0){
            return Response.error("邮箱已注册");
        }

        return Response.ok();
    }


    @GetMapping("/forgetPassword")
    @ApiOperation("忘记密码")
    public Response forgetPassword(@RequestParam String email,
                                   @RequestParam String code){

        System.out.println(email+" "+code);
        // 1. 先验证验证码
        // 得到redis中存放的验证码
        String redisCode = redisService.get("code" + email);
        // redisCode为空或者 验证码不对
        if(redisCode.isEmpty() && ! redisCode.equals(code) ){
            return Response.error("验证码错误或者无效");
        }

        // 2. 验证邮箱是否存在
        User user = userService.getByEmail(email);
        if (user != null){
            return Response.ok();
        }
        else
            return Response.error("账号不存在");
    }

    @GetMapping("/updatePassword")
    @ApiOperation("修改密码")
    public Response updatePassword(@RequestParam String email,
                                   @RequestParam String password){
        userService.updateByEmail(email,password);

        return Response.ok();
    }

}
